
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>5.2  String类的常用方法 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="5.3JavaAPI文档的使用.html" />
    
    
    <link rel="prev" href="5.1String类.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    目录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../d1z/d1z.html">
            
                <a href="../d1z/d1z.html">
            
                    
                    第一章    初识java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../d1z/1.1计算机语言.html">
            
                <a href="../d1z/1.1计算机语言.html">
            
                    
                    1.1  计算机语言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../d1z/1.2java发展史.html">
            
                <a href="../d1z/1.2java发展史.html">
            
                    
                    1.2  java发展史
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../d1z/1.3Java的特点.html">
            
                <a href="../d1z/1.3Java的特点.html">
            
                    
                    1.3  Java的特点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../d1z/1.4Java程序工作原理.html">
            
                <a href="../d1z/1.4Java程序工作原理.html">
            
                    
                    1.4  Java程序工作原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../d1z/1.5JavaSE的结构.html">
            
                <a href="../d1z/1.5JavaSE的结构.html">
            
                    
                    1.5  Java SE的结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../d1z/1.6Java开发环境搭建.html">
            
                <a href="../d1z/1.6Java开发环境搭建.html">
            
                    
                    1.6  Java开发环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../d1z/1.7第一个Java程序.html">
            
                <a href="../d1z/1.7第一个Java程序.html">
            
                    
                    1.7  第一个Java程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../d1z/1.8本章练习.html">
            
                <a href="../d1z/1.8本章练习.html">
            
                    
                    1.8  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../d2z/d2z.html">
            
                <a href="../d2z/d2z.html">
            
                    
                    第二章    数据类型和运算符
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../d2z/2.1标识符和关键字.html">
            
                <a href="../d2z/2.1标识符和关键字.html">
            
                    
                    2.1  标识符和关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../d2z/2.2变量和常量.html">
            
                <a href="../d2z/2.2变量和常量.html">
            
                    
                    2.2  变量和常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../d2z/2.3Java数据类型.html">
            
                <a href="../d2z/2.3Java数据类型.html">
            
                    
                    2.3  Java数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../d2z/2.4成员变量和局部变量.html">
            
                <a href="../d2z/2.4成员变量和局部变量.html">
            
                    
                    2.4  成员变量和局部变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../d2z/2.5Java运算符.html">
            
                <a href="../d2z/2.5Java运算符.html">
            
                    
                    2.5  Java运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../d2z/2.6Java表达式.html">
            
                <a href="../d2z/2.6Java表达式.html">
            
                    
                    2.6  Java表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../d2z/2.7本章练习.html">
            
                <a href="../d2z/2.7本章练习.html">
            
                    
                    2.7  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../d3z/d3z.html">
            
                <a href="../d3z/d3z.html">
            
                    
                    第三章    流程控制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../d3z/3.1if语句.html">
            
                <a href="../d3z/3.1if语句.html">
            
                    
                    3.1  if语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../d3z/3.2switch语句.html">
            
                <a href="../d3z/3.2switch语句.html">
            
                    
                    3.2  switch语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../d3z/3.3循环语句.html">
            
                <a href="../d3z/3.3循环语句.html">
            
                    
                    3.3  循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../d3z/3.4本章练习.html">
            
                <a href="../d3z/3.4本章练习.html">
            
                    
                    3.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../d4z/d4z.html">
            
                <a href="../d4z/d4z.html">
            
                    
                    第四章    方法与数组
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../d4z/4.1方法.html">
            
                <a href="../d4z/4.1方法.html">
            
                    
                    4.1  方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../d4z/4.2数组.html">
            
                <a href="../d4z/4.2数组.html">
            
                    
                    4.2  数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../d4z/4.3二维数组.html">
            
                <a href="../d4z/4.3二维数组.html">
            
                    
                    4.3  二维数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../d4z/4.4本章练习.html">
            
                <a href="../d4z/4.4本章练习.html">
            
                    
                    4.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="d5z.html">
            
                <a href="d5z.html">
            
                    
                    第五章    String字符串
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="5.1String类.html">
            
                <a href="5.1String类.html">
            
                    
                    5.1  String类
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.6.2" data-path="5.2String类的常用方法.html">
            
                <a href="5.2String类的常用方法.html">
            
                    
                    5.2  String类的常用方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="5.3JavaAPI文档的使用.html">
            
                <a href="5.3JavaAPI文档的使用.html">
            
                    
                    5.3  Java API文档的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="5.4StringBuffer类.html">
            
                <a href="5.4StringBuffer类.html">
            
                    
                    5.4  StringBuffer类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="5.5本章练习.html">
            
                <a href="5.5本章练习.html">
            
                    
                    5.5  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../d6z/d6z.html">
            
                <a href="../d6z/d6z.html">
            
                    
                    第六章    类和对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../d6z/6.1类和对象概述.html">
            
                <a href="../d6z/6.1类和对象概述.html">
            
                    
                    6.1  类和对象概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../d6z/6.2Java类.html">
            
                <a href="../d6z/6.2Java类.html">
            
                    
                    6.2  Java类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../d6z/6.3构造方法.html">
            
                <a href="../d6z/6.3构造方法.html">
            
                    
                    6.3  构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../d6z/6.4对象初始化过程.html">
            
                <a href="../d6z/6.4对象初始化过程.html">
            
                    
                    6.4  对象初始化过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../d6z/6.5重载.html">
            
                <a href="../d6z/6.5重载.html">
            
                    
                    6.5  重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../d6z/6.6本章练习.html">
            
                <a href="../d6z/6.6本章练习.html">
            
                    
                    6.6  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../d7z/d7z.html">
            
                <a href="../d7z/d7z.html">
            
                    
                    第七章    包和访问控制
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../d7z/7.1Java包.html">
            
                <a href="../d7z/7.1Java包.html">
            
                    
                    7.1  Java包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../d7z/7.2引用包.html">
            
                <a href="../d7z/7.2引用包.html">
            
                    
                    7.2  引用包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../d7z/7.3访问权限.html">
            
                <a href="../d7z/7.3访问权限.html">
            
                    
                    7.3  访问权限
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../d7z/7.4static关键字.html">
            
                <a href="../d7z/7.4static关键字.html">
            
                    
                    7.4  static关键字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../d7z/7.5本章练习.html">
            
                <a href="../d7z/7.5本章练习.html">
            
                    
                    7.5  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../d8z/d8z.html">
            
                <a href="../d8z/d8z.html">
            
                    
                    第八章    继承和多态
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../d8z/8.1抽象和封装.html">
            
                <a href="../d8z/8.1抽象和封装.html">
            
                    
                    8.1  抽象和封装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../d8z/8.2继承.html">
            
                <a href="../d8z/8.2继承.html">
            
                    
                    8.2  继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../d8z/8.3多态.html">
            
                <a href="../d8z/8.3多态.html">
            
                    
                    8.3  多态
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../d8z/8.4本章练习.html">
            
                <a href="../d8z/8.4本章练习.html">
            
                    
                    8.4  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../d9z/d9z.html">
            
                <a href="../d9z/d9z.html">
            
                    
                    第九章    抽象类和接口
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../d9z/9.1抽象类.html">
            
                <a href="../d9z/9.1抽象类.html">
            
                    
                    9.1  抽象类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../d9z/9.2接口.html">
            
                <a href="../d9z/9.2接口.html">
            
                    
                    9.2  接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../d9z/9.3本章练习.html">
            
                <a href="../d9z/9.3本章练习.html">
            
                    
                    9.3  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../d10z/d10z.html">
            
                <a href="../d10z/d10z.html">
            
                    
                    第十章    排序算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../d10z/10.1排序算法.html">
            
                <a href="../d10z/10.1排序算法.html">
            
                    
                    10.1  排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../d10z/10.2本章练习.html">
            
                <a href="../d10z/10.2本章练习.html">
            
                    
                    10.2  本章练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >5.2  String类的常用方法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="52--string&#x7C7B;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;">5.2  String&#x7C7B;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;</h2>
<p>&#x2003;&#x2003;&#x4EE5;&#x4E0B;&#x662F;String&#x7C7B;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x3002;</p>
<ul>
<li>public char charAt(int index)</li>
</ul>
<p>&#x2003;&#x2003;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x7D22;&#x5F15;&#x5904;&#x7684;&#x5B57;&#x7B26;&#x503C;&#x3002;</p>
<ul>
<li>public int length()</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x548C;&#x83B7;&#x53D6;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x533A;&#x522B;&#x5F00;&#xFF0C;&#x83B7;&#x53D6;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x662F;&#x901A;&#x8FC7;&#x201C;&#x6570;&#x7EC4;&#x540D;.length&#x201D;&#x83B7;&#x53D6;&#x7684;&#x3002;</p>
<ul>
<li>public int indexOf(String str)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5728;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7B2C;&#x4E00;&#x6B21;&#x51FA;&#x73B0;&#x5904;&#x7684;&#x7D22;&#x5F15;&#x3002;</p>
<ul>
<li>public int indexOf(String str,int fromIndex)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x6307;&#x5B9A;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5728;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7B2C;&#x4E00;&#x6B21;&#x51FA;&#x73B0;&#x5904;&#x7684;&#x7D22;&#x5F15;&#xFF0C;&#x4ECE;&#x6307;&#x5B9A;&#x7684;&#x7D22;&#x5F15;&#x5F00;&#x59CB;&#x641C;&#x7D22;&#x3002;</p>
<ul>
<li>public boolean equalsIgnoreCase(String another)</li>
</ul>
<p>&#x2003;&#x2003;&#x5C06;&#x6B64;String&#x4E0E;&#x53E6;&#x4E00;&#x4E2A;String&#x6BD4;&#x8F83;&#xFF0C;&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x3002;</p>
<ul>
<li>public String replace(char oldChar,char newChar)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5B83;&#x662F;&#x901A;&#x8FC7;&#x7528;newChar&#x66FF;&#x6362;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x51FA;&#x73B0;&#x7684;&#x6240;&#x6709;oldChar&#x5F97;&#x5230;&#x7684;&#x3002;</p>
<p>&#x2003;&#x2003;&#x8FD9;&#x91CC;&#x518D;&#x91CD;&#x7533;&#x4E00;&#x4E0B;&#xFF0C;String&#x7C7B;&#x65B9;&#x6CD5;&#x4E2D;&#x7684;&#x7D22;&#x5F15;&#x90FD;&#x662F;&#x4ECE;0&#x5F00;&#x59CB;&#x7F16;&#x53F7;&#x7684;&#x3002;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x8BF7;&#x6CE8;&#x610F;&#x7A0B;&#x5E8F;&#x6CE8;&#x91CA;&#xFF0C;&#x7A0B;&#x5E8F;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x56FE;5.4&#x6240;&#x793A;&#x3002;</p>
<pre><code>public class TestArrayMethod {

    public static void main(String[] args) {

        String s1 = &quot;blue bridge&quot;;

        String s2 = &quot;Blue Bridge&quot;;

        System.out.println(s1.charAt(1));                      //&#x67E5;&#x627E;&#x7B2C;2&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;l

        System.out.println(s1.length());                         //&#x6C42;s1&#x7684;&#x957F;&#x5EA6;&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;11

        System.out.println(s1.indexOf(&quot;bridge&quot;));   //&#x67E5;&#x627E;bridge&#x5B57;&#x7B26;&#x4E32;&#x5728;s1&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;5

        System.out.println(s1.indexOf(&quot;Bridge&quot;)); //&#x67E5;&#x627E;Bridge&#x5B57;&#x7B26;&#x4E32;&#x5728;s1&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x6CA1;&#x627E;&#x5230;&#x8FD4;&#x56DE;-1

        System.out.println(s1.equals(s2));                     //&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x6BD4;&#x8F83;&#xFF0C;&#x8FD4;&#x56DE;false

        System.out.println(s1.equalsIgnoreCase(s2));   //&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;&#x6BD4;&#x8F83;&#xFF0C;&#x8FD4;&#x56DE;true


        String s = &quot;&#x6211;&#x662F;&#x5B66;&#x751F;&#xFF0C;&#x6211;&#x5728;&#x5B66;java!&quot;;

        String str = s.replace(&apos;&#x6211;&apos;,&apos;&#x4F60;&apos;);                          //&#x628A;&quot;&#x6211;&quot;&#x66FF;&#x6362;&#x6210;&quot;&#x4F60;&quot;

        System.out.println(str);

    }

}
</code></pre><center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.4.png"></center>
<center> &#x56FE;5.4  String&#x7C7B;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x7EFC;&#x5408;&#x5B9E;&#x4F8B;&#x4E00;</center>  


<ul>
<li>public boolean startsWith(String prefix)</li>
</ul>
<p>&#x2003;&#x2003;&#x5224;&#x65AD;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&#x6307;&#x5B9A;&#x7684;&#x524D;&#x7F00;&#x5F00;&#x59CB;&#x3002;</p>
<ul>
<li>public boolean endsWith(String suffix)</li>
</ul>
<p>&#x2003;&#x2003;&#x5224;&#x65AD;&#x6B64;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&#x6307;&#x5B9A;&#x7684;&#x540E;&#x7F00;&#x7ED3;&#x675F;&#x3002;</p>
<ul>
<li>public String toUpperCase()</li>
</ul>
<p>&#x2003;&#x2003;&#x5C06;&#x6B64;String&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x90FD;&#x8F6C;&#x6362;&#x4E3A;&#x5927;&#x5199;&#x3002;</p>
<ul>
<li>public String toLowerCase()</li>
</ul>
<p>&#x2003;&#x2003;&#x5C06;&#x6B64;String&#x4E2D;&#x7684;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x90FD;&#x8F6C;&#x6362;&#x4E3A;&#x5C0F;&#x5199;&#x3002;</p>
<ul>
<li>public String substring(int beginIndex)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4ECE;beginIndex&#x5F00;&#x59CB;&#x5230;&#x7ED3;&#x5C3E;&#x7684;&#x65B0;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<ul>
<li>public String substring(int beginIndex,int endIndex)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x4ECE;beginIndex&#x5F00;&#x59CB;&#x5230;endIndex&#x7ED3;&#x5C3E;&#xFF08;&#x4E0D;&#x542B;endIndex&#x6240;&#x6307;&#x5B57;&#x7B26;&#xFF09;&#x7684;&#x65B0;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<ul>
<li>public String trim()</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x526F;&#x672C;&#xFF0C;&#x5FFD;&#x7565;&#x539F;&#x5B57;&#x7B26;&#x4E32;&#x524D;&#x540E;&#x7684;&#x7A7A;&#x683C;&#x3002;</p>
<p>&#x2003;&#x2003;&#x4E0B;&#x9762;&#x7EE7;&#x7EED;&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x6848;&#x4F8B;&#xFF0C;&#x8BF4;&#x660E;&#x4E0A;&#x8FF0;String&#x7C7B;&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#x3002;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x56FE;5.5&#x6240;&#x793A;&#x3002;</p>
<pre><code>public class TestStringMethod2 {

    public static void main(String[] args) {

        String fileName = &quot;20140801&#x67F3;&#x6D77;&#x9F99;Resume.docx&quot;;

        System.out.println(fileName.startsWith(&quot;2014&quot;));      //&#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&quot;2014&quot;&#x5F00;&#x5934;

        System.out.println(fileName.endsWith(&quot;docx&quot;));       //&#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&quot;docx&quot;&#x7ED3;&#x5C3E;

        System.out.println(fileName.endsWith(&quot;doc&quot;));         //&#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&quot;doc&quot;&#x7ED3;&#x5C3E;

        System.out.println(fileName.toLowerCase());              //&#x5C06;&#x5927;&#x5199;&#x53D8;&#x4E3A;&#x5C0F;&#x5199;

        System.out.println(fileName.toUpperCase());            //&#x5C06;&#x5C0F;&#x5199;&#x53D8;&#x4E3A;&#x5927;&#x5199;

        System.out.println(fileName.substring(8));    //&#x4ECE;&#x7B2C;9&#x4E2A;&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;&#x5230;&#x7ED3;&#x5C3E;&#x622A;&#x53D6;&#x5B57;&#x7B26;&#x4E32;

        System.out.println(fileName.substring(8,11));   //&#x4ECE;&#x7B2C;9&#x4E2A;&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;&#x5230;&#x7B2C;11&#x4E2A;&#x4F4D;&#x7F6E;&#x7ED3;&#x5C3E;&#x622A;&#x53D6;
                                                        //&#x5B57;&#x7B26;&#x4E32;

        String fileName2 = &quot;  20150801&#x67F3;&#x6D77;&#x9F99;Resume  .docx &quot;; 

        System.out.println(fileName2.trim());    //&#x5FFD;&#x7565;&#x539F;&#x5B57;&#x7B26;&#x4E32;&#x524D;&#x540E;&#x7684;&#x7A7A;&#x683C;

    }

}
</code></pre><center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.5.png"></center>
<center>&#x56FE;5.5  String&#x7C7B;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x7EFC;&#x5408;&#x5B9E;&#x4F8B;&#x4E8C; </center>  


<ul>
<li>public static String valueOf(&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x53C2;&#x6570;)</li>
</ul>
<p>&#x2003;&#x2003;&#x8FD4;&#x56DE;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x53C2;&#x6570;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x8868;&#x793A;&#x5F62;&#x5F0F;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code>public static String valueOf(int i)

public static String valueOf(double d)
</code></pre><p>&#x2003;&#x2003;&#x8FD9;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x662F;String&#x7C7B;&#x7684;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#xFF0C;&#x5173;&#x4E8E;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x540E;&#x9762;&#x7684;&#x8BFE;&#x7A0B;&#x4F1A;&#x8BE6;&#x7EC6;&#x4ECB;&#x7ECD;&#xFF0C;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5927;&#x5BB6;&#x6CE8;&#x610F;&#x7684;&#x662F;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x662F;&#x901A;&#x8FC7;&#x201C;&#x7C7B;&#x540D;.&#x65B9;&#x6CD5;&#x540D;&#x201D;&#x76F4;&#x63A5;&#x8C03;&#x7528;&#x7684;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code>String result = String.valueOf(100);//&#x5C06;int&#x578B;100&#x8F6C;&#x6362;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&quot;100&quot;
</code></pre><ul>
<li>public String[] split(String regex)</li>
</ul>
<p>&#x2003;&#x2003;&#x901A;&#x8FC7;&#x6307;&#x5B9A;&#x7684;&#x5206;&#x9694;&#x7B26;&#x5206;&#x9694;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD4;&#x56DE;&#x5206;&#x9694;&#x540E;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;&#x3002;</p>
<p>&#x2003;&#x2003;&#x901A;&#x8FC7;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x6848;&#x4F8B;&#xFF0C;&#x8BF4;&#x660E;&#x4E0A;&#x8FF0;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#x7684;&#x4F7F;&#x7528;&#x3002;&#x6267;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x7A0B;&#x5E8F;&#xFF0C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x5982;&#x56FE;5.6&#x6240;&#x793A;&#x3002;</p>
<pre><code>import java.util.Scanner;

public class TestStringMethod3{

    public static void main(String[] args){

        String result = String.valueOf(100);

        Scanner input = new Scanner(System.in);

        System.out.print(&quot;&#x8BF7;&#x8F93;&#x5165;&#x60A8;&#x53BB;&#x5E74;&#x4E00;&#x5E74;&#x7684;&#x85AA;&#x6C34;&#x603B;&#x548C;&#xFF1A;&quot;);

        int lastSalary = input.nextInt();

        //&#x901A;&#x8FC7;String&#x7C7B;&#x7684;&#x9759;&#x6001;&#x65B9;&#x6CD5;&#x5C06;lastSalary&#x4ECE;int&#x578B;&#x8F6C;&#x5316;&#x6210;String&#x5B57;&#x7B26;&#x4E32;

        String strSalary = String.valueOf(lastSalary);

        System.out.println(&quot;&#x60A8;&#x53BB;&#x5E74;&#x4E00;&#x5E74;&#x7684;&#x85AA;&#x6C34;&#x603B;&#x548C;&#x662F;&#xFF1A;&quot; + strSalary.length() + &quot;&#x4F4D;&#x6570;&#xFF01;&quot;);

        String date = &quot;Mary,F,1976&quot;;

        String[] splitStr = date.split(&quot;,&quot;);//&#x7528;&quot;,&quot;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x5206;&#x9694;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;

        System.out.println(&quot;Mary,F,1976&#x4F7F;&#x7528;,&#x5206;&#x9694;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF1A;&quot;);

        for(int i = 0;i &lt; splitStr.length; i++)

        {

               System.out.println(splitStr[i]);

        }       

    }

}
</code></pre><center><img src="https://labfile.oss.aliyuncs.com/library/textbook-java1/img/d5z/tu5.6.png"></center>
<center> &#x56FE;5.6  String&#x7C7B;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x7EFC;&#x5408;&#x5B9E;&#x4F8B;&#x4E09; </center>  


<p>&#x2003;&#x2003;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x7528;&#x201C;,&#x201D;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x201C;Mary,F,1976&#x201D;&#x5206;&#x9694;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;&#xFF0C;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;&#x7684;&#x957F;&#x5EA6;&#x4E3A;3&#xFF0C;&#x6BCF;&#x4E2A;&#x5143;&#x7D20;&#x5B58;&#x7684;&#x5185;&#x5BB9;&#x5206;&#x522B;&#x662F;&#x201C;Mary&#x201D;&#x3001;&#x201C;F&#x201D;&#x548C;&#x201C;1976&#x201D;&#x3002;&#x5047;&#x8BBE;&#x539F;&#x6765;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x201C;,Mary,F,1976&#x201D;&#xFF08;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x5C31;&#x662F;&#x2018;,&#x2019;&#xFF09;&#x3001;&#x201C;,Mary,F,1976&#x201D;&#xFF08;&#x7B2C;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x662F;&#x7A7A;&#x683C;&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x5B57;&#x7B26;&#x662F;&#x2018;,&#x2019;&#xFF09;&#xFF0C;&#x5176;&#x7ED3;&#x679C;&#x53C8;&#x662F;&#x5982;&#x4F55;&#x5462;&#xFF1F;&#x8BF7;&#x5927;&#x5BB6;&#x81EA;&#x884C;&#x7EC3;&#x4E60;&#x83B7;&#x5F97;&#x7ED3;&#x679C;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="5.1String类.html" class="navigation navigation-prev " aria-label="Previous page: 5.1  String类">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="5.3JavaAPI文档的使用.html" class="navigation navigation-next " aria-label="Next page: 5.3  Java API文档的使用">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"5.2  String类的常用方法","level":"1.6.2","depth":2,"next":{"title":"5.3  Java API文档的使用","level":"1.6.3","depth":2,"path":"chapters/d5z/5.3JavaAPI文档的使用.md","ref":"chapters/d5z/5.3JavaAPI文档的使用.md","articles":[]},"previous":{"title":"5.1  String类","level":"1.6.1","depth":2,"path":"chapters/d5z/5.1String类.md","ref":"chapters/d5z/5.1String类.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapters/d5z/5.2String类的常用方法.md","mtime":"2020-04-22T12:28:13.918Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-22T12:09:46.327Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

